package objects.user;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;



import objects.MessageCodes;
import objects.cluster.ClusterData;
import objects.cluster.ClusterInfo;
import objects.shared.Message;
import objects.shared.WorkPacket;

import org.eclipse.jetty.websocket.WebSocket.Outbound;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import database.ConnectionManager;
import filesystem.FileSystem;


public class UserInfo {
	public String username;
	public String password;
	public String email;
	public String diskSpace;
	public int db_id;
	
	public FileSystem fs;
	
	public ArrayList<WorkPacket> workPackets;
	public Map<Integer,ClusterInfo> clusterMap;
	
	public volatile boolean isConnected = false;
	public Outbound out;
	
	public UserInfo(objects.user.RegisterData ri)
	{
		this.username = ri.user;
		this.password = ri.pass;
		this.email = ri.email;
		this.diskSpace = "0";
		initDataStructs();
	}
	
	public UserInfo(String username,String passwd, String email, int id)
	{
		this.username = username;
		this.password = passwd;
		this.email = email;
		this.db_id = id;
		this.diskSpace = "0";
		initDataStructs();
		
	}
	
	public void initDataStructs()
	{
		workPackets = new ArrayList<WorkPacket>();
		clusterMap = Collections.synchronizedMap(new HashMap<Integer,ClusterInfo>());
		fs = new FileSystem();
	}
	
	public String getPasswd() { return password;}
	
	
	public ArrayList<ClusterData> generateClusterList() 
	{
		ArrayList<ClusterData> clusterData = new ArrayList<ClusterData>();

		Iterator<Entry<Integer,ClusterInfo>> it  = clusterMap.entrySet().iterator();
		while(it.hasNext())
		{
			ClusterData cd = new ClusterData(it.next().getValue());
			cd.setCurrentClusterStatus();
			clusterData.add(cd);
		}
		return clusterData;
	}
	
	public String getClusterDataJSONString() {
		return new Gson().toJson(generateClusterList(), new TypeToken<ArrayList<ClusterData>>() {}.getType());
	}
	
	public String getWorkPacketJSONString() {
		return new Gson().toJson(workPackets, new TypeToken<ArrayList<WorkPacket>>() {}.getType());
	}
	
	public ArrayList<ClusterInfo> getAllOnlineClusters()
	{
		ArrayList<ClusterInfo> cinfo = new ArrayList<ClusterInfo>();
		Iterator<Entry<Integer,ClusterInfo>> it  = clusterMap.entrySet().iterator();
		while(it.hasNext())
		{
			ClusterInfo ci = it.next().getValue();
			if(ci.isConnected)
				cinfo.add(ci);
				
		}
		return cinfo;
	}
	
	public void sendClusterInfo() throws Exception
	{
		sendMessage("8|"+getClusterDataJSONString());
	}
	
	public void sendLoginData() throws IOException {
		sendMessage(new Message(MessageCodes.OK,diskSpace));
		sendMessage(getClusterDataJSONString());
		sendMessage(getWorkPacketJSONString());
		sendMessage(fs.toJson());
	}
	
	public synchronized void sendMessage(Object data) throws IOException
	{
		out.sendMessage(data.toString());
	}
	
	public void disconnect()
	{
		isConnected = false;
		out.disconnect();
		out = null;
	}
}
